<?php
/**
 * Created by PhpStorm.
 * User: Sunmer
 * Date: 2019/6/5 0005
 * Time: 14:02
 * Email:Sunmer@foxmail.com
 */
namespace app\api\controller;

use think\Db;

class Bank extends BaseController{
    /**银行卡列表
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bank()
    {
        $res = request()->param();
        if (empty($res['uid']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //用户做验证
        if (!Db::table('tp_user')->where('id',$res['uid'])->find())
        {
            return $this->ajax_return('null','用户不存在','201');
        }
        //处理数据
        $result = Db::table('tp_bank')->where('uid',$res['uid'])->select();
        if ($result)
        {
            return $this->ajax_return($result,'查询成功','200');
        }else{
            return $this->ajax_return([],'没有银行卡','200');
        }
    }

    /**
     * 添加银行卡
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function join()
    {
        $res = request()->param();
        if (empty($res['uid'])||empty($res['name'])||empty($res['uname'])||empty($res['bank'])||empty($res['bank_open']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //用户做验证
        if (!Db::table('tp_user')->where('id',$res['uid'])->find())
        {
            return $this->ajax_return('null','用户不存在','201');
        }
        //银行卡做简单验证
        $chars = "/^(\d{16}|\d{19}|\d{17})$/";
        if (!preg_match($chars, $res['bank'])) {
            return $this->ajax_return('null','银行卡不正确','201');
        }
        if (Db::table('tp_bank')->where('bank',$res['bank'])->find())
        {
            return $this->ajax_return('null','银行卡已存在,请勿重复添加','201');
        }
        //处理数据
        $data = [
            'uid'           =>$res['uid'],
            'bank_name'     =>$res['name'],
            'bank_uname'    =>$res['uname'],
            'bank'          =>$res['bank'],
            'bank_open'     =>$res['bank_open'],
            'create_at' =>date('Y-m-d H:i:s',time()),
        ];
        $result = Db::table('tp_bank')->insert($data);
        if ($result)
        {
            return $this->ajax_return($data,'添加成功','200');
        }else{
            return $this->ajax_return('null','添加失败','201');
        }
    }

    /**
     * 修改银行卡
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function editb()
    {
        $res = request()->param();
        if (empty($res['id'])||empty($res['uid']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //用户做验证
        if (!Db::table('tp_user')->where('id',$res['uid'])->find())
        {
            return $this->ajax_return('null','用户不存在','201');
        }

        if(!empty($res['name'])){
            $data['bank_name'] = $res['name'];
        }
        if(!empty($res['uname'])){
            $data['bank_uname'] = $res['uname'];
        }
        if(!empty($res['bank_open'])){
            $data['bank_open'] = $res['bank_open'];
        }
        if(!empty($res['bank'])){
            //银行卡做简单验证
            $chars = "/^(\d{16}|\d{19}|\d{17})$/";
            if (!preg_match($chars, $res['bank'])) {
                return $this->ajax_return('null','银行卡不正确','201');
            }
            if (Db::table('tp_bank')->where('bank',$res['bank'])->find())
            {
                return $this->ajax_return((object)[],'银行卡已存在,请勿重复添加','201');
            }
            $data['bank'] = $res['bank'];
        }
        //处理数据
        $result = Db::table('tp_bank')->where('id',$res['id'])->update($data);
        if ($result)
        {
            return $this->ajax_return($data,'修改成功','200');
        }else{
            return $this->ajax_return((object)[],'您未更改数据','201');
        }
    }

    /**
     * 删除银行卡
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function kill()
    {
        $res = request()->param();
        if (empty($res['id'])||empty($res['uid']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //用户做验证
        if (!Db::table('tp_user')->where('id',$res['uid'])->find())
        {
            return $this->ajax_return('null','用户不存在','201');
        }
        //处理数据
        $data = [
            'id'     =>$res['id'],
            'uid'    =>$res['uid'],
        ];
        $result = Db::table('tp_bank')->where($data)->delete();
        if ($result)
        {
            return $this->ajax_return('null','删除成功','200');
        }else{
            return $this->ajax_return('null','删除失败','201');
        }
    }
}